package org.openarmory.gui;

import net.openarmory.core.domain.ArmoryCharacterXmlWriter;

import org.openarmory.gui.components.ArmoryPageRetrieveForm;
import org.openarmory.gui.pojo.ArmoryPageRetrieveBean;

import com.vaadin.Application;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

public class OpenArmoryGuiApplication extends Application {
	
	ArmoryPageRetrieveBean armoryPage;
	
	@Override
	public void init() {
		Window mainWindow = new Window("OpenArmory-gui Application");
		
		armoryPage = new ArmoryPageRetrieveBean();
		BeanItem<ArmoryPageRetrieveBean> retrieveItem = new BeanItem<ArmoryPageRetrieveBean>(armoryPage);
		
		
		final Form armoryPageRetrieveForm = new ArmoryPageRetrieveForm(retrieveItem);

		Button submit = new Button("Submit", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                try {
                	armoryPageRetrieveForm.commit();             
                	
                	ArmoryCharacterXmlWriter characterXml = new ArmoryCharacterXmlWriter();
                	characterXml.writeCharacterXml(armoryPage.getZone(), armoryPage.getRealm(), armoryPage.getCharacterName(), armoryPage.getDataType());
                	
                } catch (Exception e) {
                    // Ingnored, we'll let the Form handle the errors
                }
            }
        });
		
		armoryPageRetrieveForm.getFooter().setMargin(true);
		armoryPageRetrieveForm.getFooter().addComponent(submit);
		
		
		mainWindow.addComponent(armoryPageRetrieveForm);
//		mainWindow.addComponent(hl);
		setMainWindow(mainWindow);
	}

}
